package com.hoyin.viewtest.dependencies;

import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hoyin.viewtest.R;

import java.util.List;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
	private List<ListItem> listItems;
	public final static class ORIENTATION{
		public final static int HORIZONTAL = 1;
		public final static int VERTICAL = 0;
	}
	private int orientationFlag = ORIENTATION.VERTICAL;

	public void setOrientation(int orientation) {
		this.orientationFlag = orientation;
	}

	static class ViewHolder extends RecyclerView.ViewHolder {
		ImageView imageView;
		TextView textView;
		public ViewHolder(View view) {
			super(view);
			imageView = view.findViewById(R.id.list_item_image_view);
			textView = view.findViewById(R.id.list_item_text_view);
		}
	}
	public RecyclerViewAdapter(List<ListItem> listItems) {
		this.listItems = listItems;
	}

	@NonNull
	@Override
	public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
		View view;
		if (orientationFlag == ORIENTATION.VERTICAL)
			view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_layout, parent, false);
		else if (orientationFlag == ORIENTATION.HORIZONTAL)
			view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_horizontal_layout, parent, false);
		else
			return null;
		ViewHolder holder = new ViewHolder(view);
		return holder;
	}

	@Override
	public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
		ListItem item = listItems.get(position);
		holder.imageView.setImageResource(item.getImageId());
		holder.textView.setText(item.getName());
	}

	@Override
	public int getItemCount() {
		return listItems.size();
	}
}
